//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.web.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.live.base.domain.SystemParam;
import com.ruoyi.live.base.service.ISystemParamService;
import com.ruoyi.live.configuration.SessionInfo;
import com.ruoyi.live.platform.service.SystemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 参数管理
 */
@RestController
@RequestMapping({"/base/params"})
public class SystemParamController extends BaseController {
    @Autowired
    SystemParamService service;
    @Autowired
    ISystemParamService systemParamService;

    @GetMapping("/list")
    public TableDataInfo query(SystemParam entity) {
        this.startPage();
        QueryWrapper<SystemParam> wrapper = new QueryWrapper<>(entity);
        List<SystemParam> list = systemParamService.list(wrapper);
        return this.getDataTable(list);
    }

    /**
     * 导出参数信息列表
     */
    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "参数信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SystemParam entity)
    {
        QueryWrapper<SystemParam> wrapper = new QueryWrapper<>(entity);
        List<SystemParam> list = systemParamService.list(wrapper);
        ExcelUtil<SystemParam> util = new ExcelUtil<SystemParam>(SystemParam.class);
        util.exportExcel(response, list, "参数信息数据");
    }

    /**
     * 根据参数与编号获取详细信息
     */
    @GetMapping(value = "/{paramId}")
    public AjaxResult getInfo(@PathVariable("paramId") Long paramId)
    {
        return AjaxResult.success(systemParamService.getById(paramId));
    }

    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SystemParam entity) {
        entity.setCreateTime(System.currentTimeMillis());
        entity.setCreateUser(SessionInfo.getUserId());
        entity.setState(1);
        return toAjax(systemParamService.save(entity));
    }

    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SystemParam entity, HttpServletRequest request) {
        try {
            systemParamService.updateSystemParam(entity, request);
            return AjaxResult.success("修改成功");
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.error(exception.getMessage());
        }
    }

    @PreAuthorize("@ss.hasAnyRoles('admin')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        return toAjax(systemParamService.removeByIds(list));
    }
}
